#!/usr/bin/php
<?php

$width=500;
$interface="eth1";

function getvalue($line, $match) {
    $sp = strpos($line, $match);
    if ($sp === false)
        return false;
    $val = substr($line, $sp + strlen($match));
    $val = str_replace(array(" ", "%", "kB", "ms", "\n", "\r"), "", $val);
    return $val;
}

function execa($command) {
$line=exec($command);
$ar=explode(" ",$line);
foreach ($ar as $aar) {
if (!empty($aar) || $aar=="0")
	$car[]=$aar;
}
return $car;
}

// r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
// 0  0      0 7636540 134520 256864    0    0     1     2   11   17  0  0 100  0
echo "Collect data\n";
$vmstat=execa('vmstat 1 2 | grep 0');
$mfree=$vmstat[3];
$mbuff=$vmstat[4];
$mcache=$vmstat[5];

$ibi=$vmstat[8];
$ibo=$vmstat[9];

$sin=$vmstat[10];
$scs=$vmstat[11];

$cus=$vmstat[12];
$csy=$vmstat[13];
$cid=$vmstat[14];
$cwa=$vmstat[15];

print "vmstat ".implode(" ",$vmstat)."\n";

//Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
$ifstat1=execa("ifconfig -s {$interface} | grep {$interface}");
sleep(1);
$ifstat2=execa("ifconfig -s {$interface} | grep {$interface}");
$rxok1=$ifstat1[3];
$txok1=$ifstat1[7];
$rxok2=$ifstat2[3];
$txok2=$ifstat2[7];
$rxok=ROUND($rxok2-$rxok1);
$txok=ROUND($txok2-$txok1);

//          RX bytes:120560 (120.5 KB)  TX bytes:22038 (22.0 KB)
$ifstat1=execa("ifconfig {$interface} | grep \"RX bytes\"");
$rxbytes1=$ifstat1[1];
$rxbytes1=str_replace("bytes:","",$rxbytes1);
$txbytes1=$ifstat1[5];
$txbytes1=str_replace("bytes:","",$txbytes1);
sleep(1);
$ifstat2=execa("ifconfig {$interface} | grep \"RX bytes\"");
$rxbytes2=$ifstat2[1];
$rxbytes2=str_replace("bytes:","",$rxbytes2);
$txbytes2=$ifstat2[5];
$txbytes2=str_replace("bytes:","",$txbytes2);

$rxbw=ROUND(($rxbytes2-$rxbytes1)*8/1024768);
$txbw=ROUND(($txbytes2-$txbytes1)*8/1024768);

print "ifconfig: ".implode(" ",$ifstat1)." - ".implode(" ",$ifstat2)."\n";

exec('/var/www/scripts/get-accel-status.sh > /var/www/dumps/status');
exec('/var/www/scripts/get-accel-sessions.sh > /var/www/dumps/sessions');

$filename = "/var/www/dumps/status";
$content = file_get_contents($filename);
$accel_output = array(
    array("uptime" => "uptime:"),
    array("cpu" => "cpu:"),
    array("mem" => "mem(rss/virt):"),
    array("mempool_allocated" => "mempool_allocated:"),
    array("mempool_available" => "mempool_available:"),
    array("thread_active" => "thread_active:"),
    array("context_count" => "context_count:"),
    array("context_sleeping" => "context_sleeping:"),
    array("context_pending" => "context_pending:"),
    array("md_handler_count" => "md_handler_count:"),
    array("md_handler_pending" => "md_handler_pending:"),
    array("timer_count" => "timer_count:"),
    array("timer_pending" => "timer_pending:"),
    array("starting" => "starting:"),
    array("active" => "active:"),
    array("finishing" => "finishing:"),
    array("recv_padi" => "recv PADI:"),
    array("drop_padi" => "drop PADI:"),
    array("sent_pado" => "sent PADO:"),
    array("recv_padr" => "recv PADR(dup):"),
    array("sent_pads" => "sent PADS:"),
    array("request_count" => "request count:"),
    array("queue_length" => "queue length:"),
    array("auth_sent" => "auth sent:"),
    array("auth_lost" => "auth lost(total/5m/1m):"),
);

$contenta = explode("\n", $content);
$accel_values = array();
foreach ($contenta as $cnt) {
    foreach ($accel_output as $accel_output_item)
        foreach ($accel_output_item as $variable => $match_string) {
            if (strstr($cnt, $match_string))
                $accel_values[$variable] = getvalue($cnt, $match_string);
        }
}

echo "Update RRD\n";

$exec1="rrdtool update /var/www/graphs/cpu.rrd \"N:{$cus}:{$cus}:{$csy}:{$cwa}\"";
$exec2="rrdtool update /var/www/graphs/system.rrd \"N:{$ibi}:{$ibo}:{$sin}:{$scs}\"";
$exec3="rrdtool update /var/www/graphs/memory.rrd \"N:{$mbuff}:{$mcache}\"";
$exec4="rrdtool update /var/www/graphs/network.rrd \"N:{$rxok}:{$txok}\"";
$exec41="rrdtool update /var/www/graphs/network1.rrd \"N:{$rxbw}:{$txbw}\"";
$exec5="rrdtool update /var/www/graphs/accel.rrd \"N:{$accel_values["cpu"]}:{$accel_values["starting"]}:{$accel_values["active"]}:{$accel_values["finishing"]}\"";

print $exec1."\n".$exec2."\n".$exec3."\n".$exec4."\n".$exec41."\n".$exec5."\n";
exec($exec1);exec($exec2);exec($exec3);exec($exec4);exec($exec41);exec($exec5);

echo "Build graphs\n";

$exec1="rrdtool graph /var/www/graphs/cpu.png -a PNG ".
"--title=\"CPU\" ".
"--vertical-label \"Usage %\" ".
"--width={$width} ".
"'DEF:probe1=/var/www/graphs/cpu.rrd:cus:AVERAGE' ".
"'DEF:probe2=/var/www/graphs/cpu.rrd:cus:AVERAGE' ".
"'DEF:probe3=/var/www/graphs/cpu.rrd:csy:AVERAGE' ".
"'DEF:probe4=/var/www/graphs/cpu.rrd:cwa:AVERAGE' ".
"'AREA:probe2#ff0000:\"CPU user\":STACK' ".
"'AREA:probe3#0000ff:\"CPU system\":STACK' ".
"'AREA:probe4#880000:\"CPU wait\":STACK' ".
"'GPRINT:probe2:LAST:CPU User\: %lf' ".
"'GPRINT:probe3:LAST:CPU System\: %lf' ".
"'GPRINT:probe4:LAST:CPU Wait\: %lf' ";

$exec2="rrdtool graph /var/www/graphs/system.png -a PNG ".
"--title=\"System\" ".
"--vertical-label \"Usage\" ".
"--width={$width} ".
"'DEF:probe1=/var/www/graphs/system.rrd:ibi:AVERAGE' ".
"'DEF:probe2=/var/www/graphs/system.rrd:ibo:AVERAGE' ".
"'DEF:probe3=/var/www/graphs/system.rrd:system-in:AVERAGE' ".
"'DEF:probe4=/var/www/graphs/system.rrd:system-cs:AVERAGE' ".
"'LINE:probe1#ff0000:\"IBI\"' ".
"'LINE:probe2#00ff00:\"IBO\"' ".
"'LINE:probe3#ff00ff:\"System-IN\"' ".
"'LINE:probe4#880000:\"System-CS\"' ".
"'GPRINT:probe1:LAST:IBI\: %lf' ".
"'GPRINT:probe2:LAST:IBO\: %lf' ".
"'GPRINT:probe3:LAST:System-IN\: %lf' ".
"'GPRINT:probe4:LAST:System-CS\: %lf' ";

$exec3="rrdtool graph /var/www/graphs/memory.png -a PNG ".
"--title=\"Memory\" ".
"--vertical-label \"Usage\" ".
"--width={$width} ".
"'DEF:probe1=/var/www/graphs/memory.rrd:memory-cache:AVERAGE' ".
"'DEF:probe2=/var/www/graphs/memory.rrd:memory-buffer:AVERAGE' ".
"'AREA:probe1#ff00ff:\"Memory-Cache\":STACK' ".
"'AREA:probe2#00ff00:\"Memory-Buffer\":STACK' ".
"'GPRINT:probe1:LAST:Memory-Cache\: %lf' ".
"'GPRINT:probe2:LAST:Memory-Buffer\: %lf' ";

$exec4="rrdtool graph /var/www/graphs/network.png -a PNG ".
"--title=\"Network\" ".
"--vertical-label \"PPS\" ".
"--width={$width} ".
"'DEF:probe1=/var/www/graphs/network.rrd:packets-in-rate:AVERAGE' ".
"'DEF:probe2=/var/www/graphs/network.rrd:packets-out-rate:AVERAGE' ".
"'LINE:probe1#ff0000:\"Packets-IN\"' ".
"'LINE:probe2#00ff00:\"Packets-OUT\"' ".
"'GPRINT:probe1:LAST:Packets-IN\: %lf' ".
"'GPRINT:probe2:LAST:Packets-OUT\: %lf' ";

$exec41="rrdtool graph /var/www/graphs/network1.png -a PNG ".
"--title=\"Network\" ".
"--vertical-label \"MBits\" ".
"--width={$width} ".
"'DEF:probe1=/var/www/graphs/network1.rrd:traffic-in-rate:AVERAGE' ".
"'DEF:probe2=/var/www/graphs/network1.rrd:traffic-out-rate:AVERAGE' ".
"'LINE:probe1#0000ff:\"Traffic-IN\"' ".
"'LINE:probe2#880000:\"Traffic-OUT\"' ".
"'GPRINT:probe1:LAST:Traffic-IN\: %lf' ".
"'GPRINT:probe2:LAST:Traffic-OUT\: %lf' ";

$exec5="rrdtool graph /var/www/graphs/accel.png -a PNG ".
"--title=\"ACCEL\" ".
"--vertical-label \"Counters\" ".
"--width={$width} ".
"'DEF:probe1=/var/www/graphs/accel.rrd:cpu:AVERAGE' ".
"'DEF:probe2=/var/www/graphs/accel.rrd:starting:AVERAGE' ".
"'DEF:probe3=/var/www/graphs/accel.rrd:active:AVERAGE' ".
"'DEF:probe4=/var/www/graphs/accel.rrd:finishing:AVERAGE' ".
"'LINE:probe1#880000:\"CPU\":' ".
"'LINE:probe2#ff0000:\"Starting\"' ".
"'LINE:probe3#0000ff:\"Active\"' ".
"'LINE:probe4#00ff00:\"Finishing\"' ".
"'GPRINT:probe1:LAST:CPU\: %lf' ".
"'GPRINT:probe2:LAST:Starting\: %lf' ".
"'GPRINT:probe3:LAST:Active\: %lf' ".
"'GPRINT:probe4:LAST:Finishing\: %lf' ";

print $exec1."\n".$exec2."\n".$exec3."\n".$exec4."\n".$exec41."\n".$exec5."\n";
exec($exec1);exec($exec2);exec($exec3);exec($exec4);exec($exec41);exec($exec5);

exec("/usr/bin/bwm-ng -u bits --output html -F /var/www/dumps/bwm.html");

?>
